package cn.kgc.springbootdemo.service;

import cn.kgc.springbootdemo.entity.User;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("TokenService")
public class TokenService {

    public String getToken(User user) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 1000;//一小时有效时间

        Date end = new Date(currentTime);

        String token = JWT.create()
                .withAudience(user.getId())// 设置用户ID
                .withIssuedAt(start) //设置开始时间
                .withExpiresAt(end) //设置过期时间
                .sign(Algorithm.HMAC256(user.getPasswrod())); //设置签名
        return token;
    }
}
